import axios from "axios"
// const baseURL=process.env.NODE_ENV=='development'?'/api/admin/':'/api/admin/'  // 代理
const baseURL='https://api.shop.eduwork.cn/'
// const baseURL=process.env.NODE_ENV=='development'?'/api':'/ajax'

import { ElMessage } from "element-plus";
const http = axios.create({
  baseURL,
  timeout: 5000,
  // headers: {
  //   "X-Client-Info":
  //     '{"a":"3000","ch":"1002","v":"5.0.4","e":"1606962163804279870816257"}',
  //     "X-Host": "mall.film-ticket.film.info",
  // }
});

// 添加请求拦截器
http.interceptors.request.use(function (config) {
  if (window.sessionStorage.getItem("token")) {
    config.headers.Authorization = window.sessionStorage.getItem("token")
    return config
  }
  
  // 在发送请求之前做些什么
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
http.interceptors.response.use(function (response) {
  // console.log(response)
  // 对响应数据做点什么
  if(response.status>=200 && response.status<400){
    return response;
  }
  
  ElMessage.error("err")
  return response.data;
}, function (error) {
  // 对响应错误做点什么
 
  return Promise.reject(error);
});

export default http